/**
 * 
 */
package com.lion.jacoco.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

import com.lion.jacoco.dao.DatabasePopulator;

/**
 * DatabasePopulatorImpl.java. 2011-11-30下午3:31:47 @author LionBule.
 */
public class JacocoDatabasePopulator implements DatabasePopulator {
    private DataSource dataSource;
    private List<String> scripts;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    public void setScripts(List<String> scripts) {
        this.scripts = scripts;
    }

    @Override
    public void populate() throws SQLException {
        ResourceDatabasePopulator dp = new ResourceDatabasePopulator();
        for(String path:scripts){
            ClassPathResource newPath = new ClassPathResource(path);
            dp.addScript(newPath);
        }
        
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            dp.populate(conn);
        }finally{
            if(conn!=null){
                conn.close();
            }
        }
        
    }

}
